﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using DAO;

namespace QLDaiLy
{
    public partial class frmTiepNhanDaiLy :XtraUserControl
    {
        private string MaNhanVien;

        public frmTiepNhanDaiLy()
        {
            InitializeComponent();
        }

        public frmTiepNhanDaiLy(string MaNhanVien)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            this.MaNhanVien = MaNhanVien;
        }
        private void cleanfield()
        {
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtEmail.Text = "";
            txtTenDaiLy.Text = "";
            lookupLoaiDaiLy.EditValue = "Chọn...";
            lookupQuan.EditValue = "Chọn...";
            dateNgay.EditValue = "Chọn...";
        }
        private void LoadDanhSachQuan()
        {
            List<QuanDTO> list = new List<QuanDTO>();
            list = QuanDAO.LoadDanhSachQuan();
            lookupQuan.Properties.DataSource = list;
            lookupQuan.Properties.ValueMember = "MaQuan";
            lookupQuan.Properties.DisplayMember = "TenQuan";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;
            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenQuan");
            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupQuan.Properties.Columns.Add(col);
        }
        private void LoadDanhSachLoaiDaiLy()
        {
            List<LoaiDaiLyDTO> list = new List<LoaiDaiLyDTO>();
            list = LoaiDaiLyDAO.LoadDanhSachLoaiDaiLy();
            lookupLoaiDaiLy.Properties.DataSource = list;
            lookupLoaiDaiLy.Properties.ValueMember = "MaLoaiDaiLy";
            lookupLoaiDaiLy.Properties.DisplayMember = "TenLoaiDaiLy";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo colTenLoaiDaiLy;
            colTenLoaiDaiLy = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenLoaiDaiLy");
            colTenLoaiDaiLy.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupLoaiDaiLy.Properties.Columns.Add(colTenLoaiDaiLy);
        }
        private void frmTiepnhandl_Click(object sender, EventArgs e)
        {
            if (txtTenDaiLy.Text == "" && txtDiaChi.Text == "")
            {
                MessageBox.Show("Tên và địa chỉ không được để trống");
            }
            else
            {
                DataTable quiDinh = QuiDinhDAO.DanhSachQuiDinh();
                int quan = lookupQuan.EditValue.GetHashCode();
                int slDaiLyTrongQuan = DaiLyDAO.SoDaiLyToiDaTrongQuan(quan);
                int slDaiLyTheoQD = quiDinh.Rows[0][0].GetHashCode();

                DaiLyDTO dl = new DaiLyDTO();
                dl.MaDaiLy = DaiLyDAO.TaoMaDaiLy();
                dl.TenDaiLy = txtTenDaiLy.Text;
                dl.MaNhanVien = MaNhanVien;
                dl.MaLoaiDaiLy = Convert.ToInt32(lookupLoaiDaiLy.EditValue.ToString());
                dl.MaQuan = int.Parse(lookupQuan.EditValue.ToString());
                dl.DiaChi = txtDiaChi.Text;
                dl.DienThoai = txtDienThoai.Text;
                dl.Email = txtEmail.Text;
                dl.NgayTiepNhan = dateNgay.Text;
                dl.NoCuaDaiLy = 0;
                if (slDaiLyTheoQD <= slDaiLyTrongQuan)
                {
                    MessageBox.Show("Số Đại Lý Trong Quận vượt mức qui định");
                }
                else
                {
                    bool kq = DaiLyDAO.frmTiepNhanDaiLy(dl);
                    if (kq == true)
                    {
                        MessageBox.Show("Thêm Đại Lý thành công !");
                        cleanfield();

                    }
                    else
                    {
                        MessageBox.Show("Có lổi khi tiếp nhận đại lý");
                    }
                }
            }
        }
        private void frmTiepNhanDaiLy_Load(object sender, EventArgs e)
        {
            LoadDanhSachQuan();
            LoadDanhSachLoaiDaiLy();
        }
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            XemDanhSachDaiLy frm = new XemDanhSachDaiLy();
            tb.Controls.Add(frm, 0, 1);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

       
    }
}
